package demo.thread;

import java.util.concurrent.*;

public class FutureDemo {
    public static void main(String[] args) {

        // 创建一个包含5个线程的线程池
        ExecutorService executorService = Executors.newFixedThreadPool(5);

        // 创建一个Callable任务
        Callable<String> task = new Callable<String>() {
            public String call() {
                return "Hello from [" + Thread.currentThread().getName() + "]";
            }
        };

        // 提交任务到ExecutorService执行，并获取Future对象
        Future[] futures = new Future[10];
        for (int i = 0; i < 10; i++) {
            futures[i] = executorService.submit(task);
        }
        try {
            // 通过Future对象获取任务的结果
            for (int i = 0; i < 10; i++) {
                System.out.println(futures[i].get());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        // 关闭ExecutorService，不再接受新的任务，等待所有已提交的任务完成
        executorService.shutdown();
    }
}
